package file

import (
	"github.com/astaxie/beego/orm"
	"github.com/astaxie/beego"
	"library/common"
	"controllers"
)

type FileTaskListController struct {
	controllers.BaseRouter
}

type TaskDetailResp struct{
	Name            string  `json:"name,omitempty"`
    Files           string  `json:"files,omitempty"`
	SourceIpHosts   string  `json:"source_ip_hosts,omitempty"`
	SourceAccount   string  `json:"source_account,omitempty"`
	FileTargetPath  string  `json:"file_target_path,omitempty"`
	TargetIpHosts   string  `json:"target_ip_hosts,omitempty"`
	TargetAccount   string  `json:"target_account,omitempty"`
	Operator        string  `json:"operator,omitempty"`
	CreateTime      string  `json:"create_time,omitempty"`
	Type            int     `json:"type,omitempty"`
	Records         []FileRecord  `json:"file_record,omitempty"`
}

type FileRecord struct{
	Status          string  `json:"status,omitempty"`
	Duration        string  `json:"duration,omitempty"`
	Memo            string  `json:"memo"`
	IsFinal         int  `json:"is_final"`
}

func (c *FileTaskListController) TaskDetail () {
	taskId,_ := c.GetInt(":taskId")
	o := orm.NewOrm()
	var record []orm.Params
	var task   []orm.Params

	num,err := o.Raw("SELECT * FROM gj_file_record WHERE task_id = ?",taskId).Values(&record)
	if err != nil || num == 0{
		beego.Info(err.Error())
		c.SetJson(1, record, "获取脚本记录失败")
		return
	}

	num,err = o.Raw("SELECT * FROM gj_file_task WHERE id = ?",taskId).Values(&task)
	if err != nil || num == 0{
		beego.Info(err.Error())
		c.SetJson(1, record, "获取脚本任务失败")
		return
	}

	TaskDetailResp :=TaskDetailResp{}
	FileRecords :=[]FileRecord{}

	TaskDetailResp.Name = common.GetString(task[0]["name"])
	TaskDetailResp.Files = common.GetString(task[0]["files"])
	TaskDetailResp.SourceIpHosts = common.GetString(task[0]["source_ip_hosts"])
	TaskDetailResp.SourceAccount = common.GetString(task[0]["source_account"])
	TaskDetailResp.FileTargetPath = common.GetString(task[0]["file_target_path"])
	TaskDetailResp.TargetIpHosts = common.GetString(task[0]["target_ip_hosts"])
	TaskDetailResp.TargetAccount = common.GetString(task[0]["target_account"])
	TaskDetailResp.Operator = common.GetString(task[0]["operator"])
	TaskDetailResp.CreateTime = common.GetString(task[0]["create_time"])
	TaskDetailResp.Type = common.GetInt(task[0]["type"])

	for _,r:=range record{
		FileRecord :=FileRecord{}
		FileRecord.Status = common.GetString(r["status"])
		FileRecord.Duration = common.GetString(r["duration"])
		FileRecord.Memo = common.GetString(r["memo"])
		FileRecord.IsFinal = common.GetInt(r["is_final"])
		FileRecords=append(FileRecords,FileRecord)

	}
	TaskDetailResp.Records = FileRecords
	c.SetJson(200, TaskDetailResp, "")
	return
}

func (c *FileTaskListController) FileTaskList () {
	page, _ := c.GetInt("page", 0)
	start := 0
	length, _ := c.GetInt("length", 15)
	if page > 0 {
		start = (page - 1) * length
	}
	selectInfo := c.GetString("select_info")
	where := ""
	if selectInfo != "" {
		where = "  and( gj_file_task.name LIKE '%" + selectInfo + "%' or  gj_file_task.target_account LIKE '%" + selectInfo + "%'  or gj_file_task.operator LIKE '%" + selectInfo + "%'  )"
	}

	var task []orm.Params
	o := orm.NewOrm()
	o.Raw("SELECT distinct gj_file_task.* ,gj_file_record.is_final FROM `gj_file_task` LEFT JOIN `gj_file_record` ON gj_file_task.id = gj_file_record.task_id  WHERE 1=1 "+where+" order by id DESC  LIMIT ? ,?", start, length).Values(&task)
	var count []orm.Params
	total := 0
	o.Raw("SELECT distinct count(gj_file_record.is_final) as total FROM `gj_file_task` LEFT JOIN `gj_file_record` ON gj_file_task.id = gj_file_record.task_id WHERE 1=1 "+where).Values(&count)
	total = common.GetInt(count[0]["total"])


	c.SetJson(200,  map[string]interface{}{"total": total, "currentPage": page, "table_data": task}, "")
	return
}
